package com.dayouzi.crawler_monitor.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.Order;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据源配置类
 *
 * ps: 后续如果有需要新增的数据源，只需要在此类修改即可，添加相应的数据源配置，在此类注册好bean，
 * 并在dynamicDataSource方法中添加新增的bean配置即可
 */
@Configuration
@EnableTransactionManagement
@Order(-1) // 控制事务的执行顺序，保证开启事务的数据源优先执行
public class DataSourceConfig {

    //数据源1 onlyforcrawlerweb库
    @Bean(name = "onlyforcrawlerweb")
    @ConfigurationProperties(prefix = "spring.datasource.onlyforcrawlerwebsource")
    public DataSource onlyforcrawlerweb() {
        return DataSourceBuilder.create().build();
    }

    //数据源2 suidaobig库
    @Bean(name = "suidaobig")
    @ConfigurationProperties(prefix = "spring.datasource.suidaobigsource")
    public DataSource suidaobig() {
        return DataSourceBuilder.create().build();
    }

    //数据源3 yeji241库
    @Bean(name = "yeji241")
    @ConfigurationProperties(prefix = "spring.datasource.yeji241source")
    public DataSource yeji241() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 数据源配置
     * @return
     */
    @Primary
    @Bean(name = "dynamicDataSource")
    public DataSource dynamicDataSource() {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        Map<Object, Object> dataSourceMap = new HashMap<>();
        dataSourceMap.put("onlyforcrawlerweb", onlyforcrawlerweb());
        dataSourceMap.put("suidaobig", suidaobig());
        dataSourceMap.put("yeji241", yeji241());
        dynamicDataSource.setTargetDataSources(dataSourceMap);
        dynamicDataSource.setDefaultTargetDataSource(onlyforcrawlerweb());
        return dynamicDataSource;
    }


    /**
     * @param dataSource
     * @return
     * 数据源onlyforcrawlerweb的事务管理器
     */
    @Bean(name = "onlyforcrawlerwebTransactionManager")
    @Primary
    public DataSourceTransactionManager onlyforcrawlerwebTransactionManager(@Qualifier("onlyforcrawlerweb") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }


    /**
     * @param dataSource
     * @return
     * 数据源suidaobig的事务管理器
     */
    @Bean(name = "suidaobigTransactionManager")
    public DataSourceTransactionManager suidaobigTransactionManager(@Qualifier("suidaobig") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    /**
     * @param dataSource
     * @return
     * 数据源yeji241的事务管理器
     */
    @Bean(name = "yeji241TransactionManager")
    public DataSourceTransactionManager yeji241TransactionManager(@Qualifier("yeji241") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    /**
     * 事务管理
     * @return
     */
    @Bean
    public PlatformTransactionManager TransactionManager() {
        return new DataSourceTransactionManager(dynamicDataSource());
    }


}
